
#!/bin/bash

SET=instructions.conf


file=${1}
mem=0
mudi=0
out=0

getCPI(){
    case "$1" in
	"mem")
	    mem=$(echo "${mem} + ${2}" | bc)
	    echo 10
	    
	    ;;
        "mudi") 
	    
	    mudi=$(echo "${mudi} + ${2}" | bc)
	    echo 20
	    ;;
	"out") 
	    out=$(echo "${out} + ${2}" | bc)
	    echo 1
	    ;;
    esac
}

count=0


while read line
do
    cmd=$(echo ${line} | cut -d "|" -f 1)
    qtd=$(echo ${line} | cut -d "|" -f 2)
    tipo=$(grep -F -m 1 ${cmd} ${SET} | cut -d "|" -f 2)
    if [ "${qtd}" != "0" ]
    then
	getCPI ${tipo} ${qtd}  > /dev/null
    fi
    cpi=$(getCPI ${tipo} 0)
    cpi=$(echo "${cpi} * ${qtd}" | bc)
    count=$(echo "${count} + ${cpi}" | bc)
    
done < ${file}

echo ${count}

echo "${mem} + ${mudi} + ${out}"